package ejb.entities;

import java.io.Serializable;

import javax.persistence.*;

import ejb.dto.PacchettoDTO;
import ejb.dto.ProdottoBaseDTO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * The persistent class for the packs database table.
 * 
 */
@Entity
@Table(name = "packs")
@NamedQueries({
		@NamedQuery(name = "Pack.findAll", query = "SELECT p FROM Pack p"),
		@NamedQuery(name = "Pack.findByID", query = "SELECT p FROM Pack p WHERE p.packsid LIKE :idPack"),
		@NamedQuery(name = "Pack.findPackCreatedByUser", query = "SELECT p FROM Pack p WHERE p.user.userid LIKE :userid"),
		@NamedQuery(name = "Pack.findByDates", query = "SELECT p FROM Pack p  WHERE p.startdate = :dataInizio  AND p.enddate = :dataFine") })
public class Pack implements Serializable {
	private static final long serialVersionUID = 1L;

	// attributi per le query
	public static final String FINDALL = "Pack.findAll";
	public static final String FIND_BYID = "Pack.findByID";
	public static final String FINDPACKCREATEDBYUSER = "Pack.findPackCreatedByUser";
	public static final String FIND_DATES = "Pack.findByDates";

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int packsid;

	private String description;

	private int discount;

	@Temporal(TemporalType.DATE)
	private Date enddate;

	private String name;

	private float price;

	@Temporal(TemporalType.DATE)
	private Date startdate;

	// bi-directional many-to-many association to Basicproduct
	@ManyToMany
	@JoinTable( name = "basicproduct_pack" , 
			    joinColumns = { @JoinColumn( name = "packid" )}, 
				inverseJoinColumns = { @JoinColumn( name = "basicproductid" ) })
	private List<Basicproduct> basicproducts;

	// bi-directional many-to-many association to Giftlist
	@ManyToMany
	@JoinTable(name = "giftlist_pack", joinColumns = { @JoinColumn(name = "packid") }, inverseJoinColumns = { @JoinColumn(name = "giftlistid") })
	private List<Giftlist> giftlists;

	// bi-directional many-to-one association to User
	@ManyToOne
	@JoinColumn(name = "userid")
	private User user;

	// bi-directional many-to-one association to UserPack
	@OneToMany(mappedBy = "pack")
	private List<UserPack> userPacks;

	public Pack() {
		this.userPacks = new ArrayList<UserPack>();
		this.giftlists = new ArrayList<Giftlist>();
		this.basicproducts = new ArrayList<Basicproduct>();
	}


	public Pack(PacchettoDTO pDTO) {
		this.description = pDTO.getDescrizione();
		this.discount = pDTO.getSconto();
		this.enddate = pDTO.getDataFine();
		this.startdate = pDTO.getDataInizio();
		this.name = pDTO.getNome();
		this.price = pDTO.getPrezzo();
		this.userPacks = new ArrayList<UserPack>();
		this.giftlists = new ArrayList<Giftlist>();
		this.basicproducts = new ArrayList<Basicproduct>();
	}

	public int getPacksid() {
		return this.packsid;
	}

	public void setPacksid(int packsid) {
		this.packsid = packsid;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public int getDiscount() {
		return this.discount;
	}

	public void setDiscount(int discount) {
		this.discount = discount;
	}

	public Date getEnddate() {
		return this.enddate;
	}

	public void setEnddate(Date enddate) {
		this.enddate = enddate;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public float getPrice() {
		return this.price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public Date getStartdate() {
		return this.startdate;
	}

	public void setStartdate(Date startdate) {
		this.startdate = startdate;
	}

	public List<Basicproduct> getBasicproducts() {
		return this.basicproducts;
	}

	public void setBasicproducts(List<Basicproduct> basicproducts) {
		this.basicproducts = basicproducts;
	}

	public List<Giftlist> getGiftlists() {
		return this.giftlists;
	}

	public void setGiftlists(List<Giftlist> giftlists) {
		this.giftlists = giftlists;
	}

	public User getUser() {
		return this.user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<UserPack> getUserPacks() {
		return this.userPacks;
	}

	public void setUserPacks(List<UserPack> userPacks) {
		this.userPacks = userPacks;
	}

	public UserPack addUserPack(UserPack userPack) {
		getUserPacks().add(userPack);
		userPack.setPack(this);

		return userPack;
	}

	public UserPack removeUserPack(UserPack userPack) {
		getUserPacks().remove(userPack);
		userPack.setPack(null);

		return userPack;
	}
	
	private List<Basicproduct> convertToEntity(List<ProdottoBaseDTO> pbDTO) {
		List<Basicproduct> pb = new ArrayList<Basicproduct>();

		for(ProdottoBaseDTO p : pbDTO)
		{
			pb.add( new Basicproduct( p ));
		}

		return pb;
	}

}